package cn.moukou.sbzz.interceptor;

import cn.moukou.sbzz.util.JwtUtil;
import io.jsonwebtoken.SignatureException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Component
public class EnterpriseInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("sbzz-token");
        String method = request.getMethod();
        if(StringUtils.isEmpty(token)){
            System.out.println(method);
            throw new SignatureException(JwtUtil.getHeader()+ "不能为空");
        }

        Map<String, Object> map = JwtUtil.getInfoFromToken(token);
        if(map.get("role").equals("admin") || map.get("role").equals("enterprise")) {
            return true;
        }
        return false;
    }
}
